package com.example.audiostreaming.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.audiostreaming.R;
import com.example.audiostreaming.activity.DetailActivity;
import com.example.audiostreaming.adapters.AdapterAlbumRv;
import com.example.audiostreaming.base.BaseFragment;
import com.example.audiostreaming.implement.ImplDetailLogic;
import com.example.audiostreaming.implement.ImplRecommendLogic;
import com.example.audiostreaming.interfaces.IRecommendViewCallback;
import com.example.audiostreaming.utils.UtilUI;
import com.example.audiostreaming.views.ViewUILoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.List;

/**
 * 推荐界面UI视图层
 * 推荐片段
 */
public class FragmentRecommend extends BaseFragment {
    private static final String TAG = "FragmentRecommend";
    private View mSubViewRecommend;//推荐子视图
    private RecyclerView mRecommendRv;
    private AdapterAlbumRv mAdapterRecommendAlbumRv;
    private ImplRecommendLogic mInstanceImplRecommendLogic;
    private RecommendViewCallback mRecommendViewCallback;
    private ViewUILoader mUiLoader;//加载器


    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, final ViewGroup container)  {

        mUiLoader = new ViewUILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSussessView(layoutInflater,container);
            }
        };


        //获取推荐界面逻辑实现层的单例对象
        mInstanceImplRecommendLogic = ImplRecommendLogic.getInstanceImplRecommendLogic();

        //设置注册通知UI界面更新的回调
        mRecommendViewCallback = new RecommendViewCallback();
        mInstanceImplRecommendLogic.registerViewCallback(mRecommendViewCallback);


        //获取推荐专辑列表数据
        mInstanceImplRecommendLogic.getRecommendAlbumListData();

        //mUiLoader与父类解绑，不允许重复绑定
        if (mUiLoader.getParent() instanceof ViewGroup) {
            ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
        }

        mUiLoader.setOnRetryClickListener(new OnRetryClickListener());//设置重新点击监听器，当发生网络错误时，可用到
        //返回view,给界面显示
        return mUiLoader;
    }

    private View createSussessView(LayoutInflater layoutInflater, ViewGroup container) {
        //view加载完成
        mSubViewRecommend = layoutInflater.inflate(R.layout.fragment_recommend, container, false);

        //RecyclerView
        //1.找到控件
        mRecommendRv = mSubViewRecommend.findViewById(R.id.recommend_Rv);
        TwinklingRefreshLayout overScrollView = mSubViewRecommend.findViewById(R.id.over_scroll_view);
        overScrollView.setPureScrollModeOn();
        //2.设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mRecommendRv.setLayoutManager(linearLayoutManager);
        //设置ItemView条目视图上下左右间距大小
        mRecommendRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = UtilUI.dip2px(view.getContext(), 0);//dp转成px像素
                outRect.bottom = UtilUI.dip2px(view.getContext(), 3);
                outRect.left = UtilUI.dip2px(view.getContext(), 3);
                outRect.right = UtilUI.dip2px(view.getContext(), 3);
            }
        });
        //3.设置设配器
        mAdapterRecommendAlbumRv = new AdapterAlbumRv();
        mAdapterRecommendAlbumRv.setOnAlbumItemClickListener(new OnRecommendAlbumItemClickListener() );
        mRecommendRv.setAdapter(mAdapterRecommendAlbumRv);
        return mSubViewRecommend;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消设置注册通知UI界面更新的回调，避免数据被一直持有引用，垃圾回收器无法回收，造成内存泄漏
        if (mInstanceImplRecommendLogic != null) {
            mInstanceImplRecommendLogic.unRegisterViewCallback(mRecommendViewCallback);
            mInstanceImplRecommendLogic = null;
        }
    }








    /**
     * 推荐专辑界面UI视图回调的内部类
     */
    private class RecommendViewCallback implements IRecommendViewCallback{
        /**
         * 获取到推荐专辑列表数据时，该方法被调用
         * 数据回来后，推荐专辑列表视图UI设置相应数据
         * @param resultRecommendAlbumListData
         */
        @Override
        public void onSetRecommendAlbumListData(List<Album> resultRecommendAlbumListData) {
            //把数据设置给RecyclerView的适配器，并且更新UI
            mAdapterRecommendAlbumRv.setData(resultRecommendAlbumListData);
            mUiLoader.upDateStatus(ViewUILoader.UIStatus.SUCCESS);
        }

        @Override
        public void onPullUpRecommendAlbumListData(List<Album> resultRecommendAlbumListData) {

        }

        @Override
        public void onDropDownRecommendAlbumListData(List<Album> resultRecommendAlbumListData) {

        }




        @Override
        public void onNetWorkErrorRecommendAlbumListData() {
            mUiLoader.upDateStatus(ViewUILoader.UIStatus.NETWORK_ERROR);
        }

        @Override
        public void onEmptyRecommendAlbumListData() {
            mUiLoader.upDateStatus(ViewUILoader.UIStatus.EMPTY);
        }

        @Override
        public void onLoadingRecommendAlbumListData() {
            mUiLoader.upDateStatus(ViewUILoader.UIStatus.LOADING);
        }
    }








    private class OnRetryClickListener implements ViewUILoader.OnRetryClickListener {
        @Override
        public void onRetryClick() {
            //网络错误，点击重试，重新获取数据
            if (mInstanceImplRecommendLogic != null) {
                mInstanceImplRecommendLogic.getRecommendAlbumListData();
            }
        }
    }

    private class OnRecommendAlbumItemClickListener implements AdapterAlbumRv.OnAlbumItemClickListener {
        /**
         * 推荐专辑条目被点击，跳转到详情界面
         * @param position 推荐专辑条目位置
         * @param album    推荐专辑条目数据
         */
        @Override
        public void onAlbumItemClick(int position, Album album) {
            //将推荐专辑条目数据设置为详情专辑，传给ImplDetailLogic详情逻辑实现层进行处理
            ImplDetailLogic.getInstanceImplDetailLogic().setDetailAlbum(album);
            Intent intent = new Intent(getContext(), DetailActivity.class);
            startActivity(intent);
        }
    }

}
